{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Strings\n",
    "\n",
    "**Time**\n",
    "- Teaching: 10 min\n",
    "- Exercises: 20 min\n",
    "\n",
    "**Questions**:\n",
    "- \"How do I manipulate strings (text)?\"\n",
    "\n",
    "**Learning Objectives**:\n",
    "- Become familiar with the [string](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#string) [type](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#type) and its methods\n",
    "* * * * *\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## We can do things with strings\n",
    "\n",
    "* We've already seen some operations that can be done with strings."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "first_name = \"Johan\"\n",
    "last_name = \"Gambolputty\"\n",
    "full_name = first_name + last_name\n",
    "print(full_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Remember that computers don't understand context."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "full_name = first_name + \" \" + last_name\n",
    "print(full_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Strings are made up of sub-strings\n",
    "\n",
    "* You can think of strings as a [sequence](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#sequence) of smaller strings or characters. \n",
    "* We can access a piece of that sequence using `[]`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "full_name[1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Gotcha** - Python (and many other langauges) start counting from 0."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "full_name[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "full_name[4]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## You can slice strings using  `[ : ]`\n",
    "\n",
    "* if you want a range (or \"slice\") of a sequence, you get everything *before* the second index:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "full_name[0:4]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "full_name[0:5]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* You can see some of the logic for this when we consider implicit indices."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "full_name[:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "full_name[5:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## String Have Methods"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* There are other operations defined on string data. These are called **string [methods](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#method)**. \n",
    "* IPython lets you do tab-completion after a dot ('.') to see what methods an [object](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#object) (i.e., a defined variable) has to offer. Try it now!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "str."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "* Let's look at the upper method. What does it do? Lets take a look at the documentation. IPython lets us do this with a question mark ('?') before *or* after an object (again, a defined variable)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "str.upper?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So we can use it to upper-caseify a string. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "full_name.upper()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You have to use the parenthesis at the end because upper is a method of the string class.\n",
    "\n",
    "Don't forget, simply calling the method does not change the original variable, you must reassign the variable:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(full_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "full_name = full_name.upper()\n",
    "print(full_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For what its worth, you don't need to have a variable to use the upper() method, you could use it on the string itself."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"Johann Gambolputty\".upper()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What do you think should happen when you take upper of an int?  What about a string representation of an int?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Challenge 1: Write your name\n",
    "\n",
    "1. Make two string variables, one with your first name and one with your last name.\n",
    "2. Concatenate both strings to form your full name and [assign](https://github.com/dlab-berkeley/python-intensive/blob/master/Glossary.md#assign) it to a variable.\n",
    "3. Assign a new variable that has your full name in all upper case.\n",
    "4. Slice that string to get your first name again."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Challenge 2: Try seeing what the following string methods do:\n",
    "\n",
    "    * `split`\n",
    "    * `join`\n",
    "    * `replace`\n",
    "    * `strip`\n",
    "    * `find`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Challenge 3: Working with strings"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Below is a string of Edgar Allen Poe's \"A Dream Within a Dream\":"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "poem = '''Take this kiss upon the brow!\n",
    "And, in parting from you now,\n",
    "Thus much let me avow —\n",
    "You are not wrong, who deem\n",
    "That my days have been a dream;\n",
    "Yet if hope has flown away\n",
    "In a night, or in a day,\n",
    "In a vision, or in none,\n",
    "Is it therefore the less gone?  \n",
    "All that we see or seem\n",
    "Is but a dream within a dream.\n",
    "\n",
    "I stand amid the roar\n",
    "Of a surf-tormented shore,\n",
    "And I hold within my hand\n",
    "Grains of the golden sand —\n",
    "How few! yet how they creep\n",
    "Through my fingers to the deep,\n",
    "While I weep — while I weep!\n",
    "O God! Can I not grasp \n",
    "Them with a tighter clasp?\n",
    "O God! can I not save\n",
    "One from the pitiless wave?\n",
    "Is all that we see or seem\n",
    "But a dream within a dream?'''"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What is the difference between `poem.strip(\"?\")` and `poem.replace(\"?\", \"\")` ?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "At what index does the word \"*and*\" first appear? Where does it last appear?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "How can you answer the above accounting for upper- and lowercase?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# Keypoints\n",
    "\n",
    "- Some mathematical operators can be used on strings\n",
    "- Strings can be indexed, Python indexing always starts at 0!\n",
    "- Strings, and other types, have their own methods, which are called using dots after the variable, and then the method name."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
